-- card: 63445 from stack: in -- bmap block id: 0 -- flags: 0000 -- background id: 4755 -- name: -- part 1 (field) -- low flags: 00 -- high flags: 0007 -- rect: left=30 top=78 right=296 bottom=478 -- title width / last selected line: 0 -- icon id / first selected line: 0 / 0 -- text alignment: 0 -- font id: 4 -- text size: 9 -- style flags: 0 -- line height: 12 -- part name: -- part contents for background part 7 ----- text ----- 206 -- part contents for card part 1 ----- text ----- /* * FILE: screen.c * AUTHOR: R. Gonzalez * CREATED: Aug. 5, 1990 * * screen.c contains several Mac-specific routines to supplement * a typical stdio-type application with a graphics window. The * Macintosh Toolbox is a set of routines in ROM to produce the * graphical user interface. These Toolbox functions are defined in * MacHeaders. They are distinguished from my own functions by * beginning with a capital letter. * * Think C produces the "generic" console environment when a stdio * function call appears before the standard Toolbox initializations. * This appears to allow both input and output in the console window. * The console comes to the front automatically when written to. * This approach requires SelectWindow or BringToFront be used to * bring the graphics window to the front. These routines do this * each time a line or circle is drawn, automatically reactivating * the graphics window. They also will do an erase_graphics() * because the console will wash out the previous contents of the * graphics window. */ # include "screen.h" # include # include # define BACKGROUND BLACK /****************************************************************** * Transform view coordinate x to screen coordinates ******************************************************************/ int Screen::transform_x(double x) { return (int) ((x+view_x)*(double) screen_width/view_width); } /****************************************************************** * Transform view coordinate y to screen coordinates ******************************************************************/ int Screen::transform_y(double y) { return (int) ((view_height-view_y-y) * (double) screen_height/view_height); } /****************************************************************** * Initialize screen. Derived classes must create windows and * initialize screen_width, screen_height instance variables * before calling the inherited method. ******************************************************************/ boolean Screen::init(void) { set_view(2.,2.,1.,1.); color(WHITE); erase_graphics(); return TRUE; } /****************************************************************** * Bring graphics to front. This is up to the derived class, * which may then call inherited method to erase old graphic. ******************************************************************/ void Screen::graphics_to_front(void) { erase_graphics(); } /****************************************************************** * Bring console to front This is up to the derived class. ******************************************************************/ void Screen::console_to_front(void) { } /****************************************************************** * Return screen aspect ratio: width/height ******************************************************************/ double Screen::get_aspect_ratio(void) { return (double) screen_width/(double) screen_height; } /****************************************************************** * Change view coordinate system for draw_line(), draw_circle(), * etc. This is in terms of the view width, height, and origin of * the desired graphics window. In terms of screen coordinates * the graphics window uses the whole screen. ******************************************************************/ void Screen::set_view(double width,double height, double x,double y) { view_width = width; view_height = height; view_x = x; view_y = y; } /****************************************************************** * Turn cursor on. This is up to the derived class. ******************************************************************/ void Screen::cursor_on(void) { } /****************************************************************** * Turn cursor off. This is up to the derived class. ******************************************************************/ void Screen::cursor_off(void) { } /****************************************************************** * Sets the current drawing color. Overriding method should * set color of drawing pen and call inherited method. ******************************************************************/ void Screen::color(int x) { present_color = x; } /****************************************************************** * Sets graphics window to background color. Up to derived class. ******************************************************************/ void Screen::erase_graphics(void) { } /****************************************************************** * draw_line() is used to draw lines using view coordinates. ******************************************************************/ void Screen::draw_line(double x1,double y1,double x2,double y2) { move_to(x1,y1); draw_to(x2,y2); } /****************************************************************** * Move present pen position to new position using view * coordinates. Nothing is drawn. Derived class should use * transform_x() and transform_y() to convert to screen coords. ******************************************************************/ void Screen::move_to(double x,double y) { } /****************************************************************** * Draw from present pen position to new position using view * coordinates. Derived class should use transform_x() and * transform_y() to convert to screen coordinates. ******************************************************************/ void Screen::draw_to(double x,double y) { } /****************************************************************** * draw_circle() draws a circle using view coordinates. Derived * class should use transform_x() and transform_y() to convert to * screen coordinates. ******************************************************************/ void Screen::draw_circle(double x,double y,double r) { } /****************************************************************** * fill_circle() draws a circle using view coordinates. The * circle is filled with the present pen color. Derived class * should use transform_x() and transform_y() to convert to screen * coordinates. ******************************************************************/ void Screen::fill_circle(double x,double y,double r) { } /****************************************************************** * mouse_button_is_down() checks whether the mouse button is down, * returns TRUE if so, FALSE if not. Up to derived class. Make * sure to override this if you use Screen::wait(), or override * Screen::wait(). (Else you'll get an infinite "wait"!) ******************************************************************/ boolean Screen::mouse_button_is_down(void) { return FALSE; } /****************************************************************** * get_mouse_location() returns the mouse coordinates (via its * arguments) in terms of the view coordinate system. Derived * class must get the screen coords of the mouse, and use the in- * verse transformation of that in transform_x() and transform_y(). ******************************************************************/ void Screen::get_mouse_location(double *x_ptr,double *y_ptr) { } /****************************************************************** * Wait until button is pressed. ******************************************************************/ void Screen::wait(void) { while (!mouse_button_is_down()) ; while (mouse_button_is_down()) ; } /*------------------- Mac_Screen methods -----------------------*/ # define NIL_POINTER 0L # define MOVE_TO_FRONT -1L # define REMOVE_ALL_EVENTS 0 # define TITLE "\pGraphics" # define VISIBLE 1 # define NO_GO_AWAY 0 # define NIL_REF_CON NIL_POINTER /****************************************************************** * You must call init() at the beginning of main(). Likely don't * need all these initializations; most are taken from Mark & * Reed's "Macintosh Programming Primer", Addison-Wesley, 1989 ******************************************************************/ boolean Mac_Screen::init(void) { /* Need a stdio function call to produce the generic console * environment BEFORE the remaining inits: */ printf("\n"); console_window = FrontWindow(); InitGraf(&thePort); InitFonts(); FlushEvents(everyEvent,REMOVE_ALL_EVENTS); /* Commented out InitWindows to preserve the menu bar. * May be dangerous? */ /* InitWindows(); */ InitMenus(); TEInit(); InitDialogs(NIL_POINTER); InitCursor(); CrossCursor = GetCursor(crossCursor); HideCursor(); graphics_window = NewWindow(NIL_POINTER,&(screenBits.bounds), TITLE,VISIBLE,plainDBox,MOVE_TO_FRONT,NO_GO_AWAY,NIL_REF_CON); SetPort(graphics_window); screen_width = graphics_window->portRect.right - graphics_window->portRect.left; screen_height = graphics_window->portRect.bottom - graphics_window->portRect.top; return inherited::init(); } /****************************************************************** * Bring graphics to front. Never needed with Mac version, * since this is done by the drawing routines. ******************************************************************/ void Mac_Screen::graphics_to_front(void) { SelectWindow(graphics_window); inherited::graphics_to_front(); } /****************************************************************** * Bring console to front. Never needed with Mac version, since * this happens automatically when stdio is used. ******************************************************************/ void Mac_Screen::console_to_front(void) { SelectWindow(console_window); } /****************************************************************** * Turn cursor on ******************************************************************/ void Mac_Screen::cursor_on(void) { SetCursor(*CrossCursor); ShowCursor(); } /****************************************************************** * Turn cursor off ******************************************************************/ void Mac_Screen::cursor_off(void) { HideCursor(); } /****************************************************************** * color() sets the current drawing color. The colors are defined * in Macheaders. ******************************************************************/ void Mac_Screen::color(int x) { inherited::color(x); switch (x) { case 0: ForeColor(blackColor); break; case 1: ForeColor(whiteColor); break; case 2: ForeColor(redColor); break; case 3: ForeColor(yellowColor); break; case 4: ForeColor(greenColor); break; case 5: ForeColor(blueColor); break; case 6: ForeColor(cyanColor); break; case 7: ForeColor(magentaColor); break; default: break; } } /****************************************************************** * erase_graphics() calls the appropriate Mac Toolbox function to * make the entire screen the background color. ******************************************************************/ void Mac_Screen::erase_graphics(void) { int temp_color; if (graphics_window != FrontWindow()) SelectWindow(graphics_window); temp_color = present_color; color(BACKGROUND); FillRect(&(graphics_window->portRect),black); color(temp_color); } /****************************************************************** * Move present pen position to new position using view * coordinates. After transformation into screen coordinates the * Macintosh QuickDraw routines are called. ******************************************************************/ void Mac_Screen::move_to(double x,double y) { int screen_x, screen_y; if (graphics_window != FrontWindow()) { SelectWindow(graphics_window); erase_graphics(); } screen_x = transform_x(x); screen_y = transform_y(y); MoveTo(screen_x,screen_y); } /****************************************************************** * Draw from present pen position to new position using view * coordinates. After transformation into screen coordinates the * Macintosh QuickDraw routines are called. ******************************************************************/ void Mac_Screen::draw_to(double x,double y) { int screen_x, screen_y; if (graphics_window != FrontWindow()) { SelectWindow(graphics_window); erase_graphics(); } screen_x = transform_x(x); screen_y = transform_y(y); LineTo(screen_x,screen_y); } /****************************************************************** * draw_circle() draws a circle using the Mac Toolbox routines. It * accepts view coordinates. ******************************************************************/ void Mac_Screen::draw_circle(double x,double y,double r) { Rect myRect, *myRectPtr; int screen_x, screen_y, screen_r; if (graphics_window != FrontWindow()) { SelectWindow(graphics_window); erase_graphics(); } screen_x = transform_x(x); screen_y = transform_y(y); screen_r = (int) (r*(double) screen_width/view_width); myRectPtr = &myRect; myRectPtr->left = screen_x-screen_r; myRectPtr->right = screen_x+screen_r; myRectPtr->top = screen_y-screen_r; myRectPtr->bottom = screen_y+screen_r; FrameOval(&myRect); } /****************************************************************** * fill_circle() draws a circle using the Mac Toolbox routines. It * accepts view coordinates. The circle is filled with the present * pen color ******************************************************************/ void Mac_Screen::fill_circle(double x,double y,double r) { Rect myRect, *myRectPtr; int screen_x, screen_y, screen_r; if (graphics_window != FrontWindow()) { SelectWindow(graphics_window); erase_graphics(); } screen_x = transform_x(x); screen_y = transform_y(y); screen_r = (int) (r*(double) screen_width/view_width); myRectPtr = &myRect; myRectPtr->left = screen_x-screen_r; myRectPtr->right = screen_x+screen_r; myRectPtr->top = screen_y-screen_r; myRectPtr->bottom = screen_y+screen_r; PaintOval(&myRect); } /****************************************************************** * mouse_button_is_down() checks whether the mouse button is down, * returns TRUE if so, FALSE if not. ******************************************************************/ boolean Mac_Screen::mouse_button_is_down(void) { return Button(); } /****************************************************************** * get_mouse_location() returns the mouse coordinates in terms of * the view coordinate system, using the appropriate Mac Toolbox * functions and the inverse transformation of that in * transform_x() and transform_y(). ******************************************************************/ void Mac_Screen::get_mouse_location(double *x_ptr,double *y_ptr) { Point mouseLoc; GetMouse(&mouseLoc); *x_ptr = (double) mouseLoc.h/ (double) screen_width*view_width-view_x; *y_ptr = view_height-view_y-view_height* (double) mouseLoc.v/(double) screen_height; } -- part contents for background part 4 ----- text ----- File 2 of 3. Method definitions: